<?php
class TOCore{
    /**
     * This loads the requested page from a .php file
     * @param $page - string
     */
    public static function load($page = ''){

        if($page == '') $page = 'index';

        if(file_exists($page.'.php')) include $page.'.php';
        else echo '<p>Error:file not found</p>';
        $parts = explode('/',$page);
        return $parts[count($parts)-1];
    }

    /**
     * The main function for the TOCore class. This is loaded by default
     */
    public static function main()
    {
        $queryArray = array();
		$final = "";
		if(isset($_SERVER['REDIRECT_QUERY_STRING']))
		{
			/*We are here probably by a redirect, so load the page*/
			parse_str($_SERVER['REDIRECT_QUERY_STRING'],$queryArray);			
			$page = $queryArray['page'];
			$final = self::load($page);
		}else
		{
			/*We are here organic, so we should figure the name of the file*/
			$final = basename($_SERVER['PHP_SELF'],".php");
		}
		
		/*We have the name, let's run the script*/
        if(class_exists($final))
        {
            if(method_exists($final, 'main'))
			{
				/* evaluate the main method */
                eval("{$final}::main();");
			}
            else
			{
				/* No main method. The fun is over */
			    echo 'There is no method main() in '.$final.'.php';
			}
        }else
        {
			/* The class is not declared (corectly) */
            echo "<p>Can't find the main class</p>";
        }
    }
}

TOCore::main();